package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.annotation.UserInfoParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/query")
    public Object query(StrategyQueryObject qo){

        Page page = strategyService.query(qo);


        return JsonResult.success(page);
    }

    @GetMapping("/themes")
    public Object themes(){

        List<StrategyTheme> list = strategyThemeService.findAll();


        return JsonResult.success(list);
    }

    @GetMapping("/detail")
    public Object detail(String id){
        Strategy strategy = strategyService.find(id);
        //每次访问阅读量+1
        strategyStatisVORedisService.viewnumIncr(id);
        return JsonResult.success(strategy);
    }

    @RequiredLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, @UserInfoParam UserInfo userInfo){

        strategyCommentService.addComment(strategyComment,userInfo);
        strategyStatisVORedisService.replynumIncr(strategyComment.getStrategyId());
        return JsonResult.success();
    }


    @GetMapping("/comments")
    public Object queryStrategyComments(StrategyCommentQueryObject qo){
        Page page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }


    @RequiredLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, @UserInfoParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid,userInfo);
        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        StrategyStatisVO vo = strategyStatisVORedisService.getStrategyStatisVO(sid);
        return JsonResult.success(vo);
    }


    @RequiredLogin
    @PostMapping("/favor")
    public Object strategyFavorExist(String sid, @UserInfoParam UserInfo userInfo){
        boolean ret = strategyStatisVORedisService.strategyFavorExist(sid,userInfo);
        Map<String, Object> map = new HashMap<>();
        map.put("ret",ret);
        return JsonResult.success(map);
    }


    @RequiredLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbupExist(String sid, @UserInfoParam UserInfo userInfo){
        boolean ret = strategyStatisVORedisService.strategyThumbupExist(sid,userInfo.getId());
        Map<String, Object> map = new HashMap<>();
        map.put("ret",ret);
        return JsonResult.success(map);
    }

}
